﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StationClient.Dialog
{
    public partial class DialogWorker : Form
    {
        STATION _sta = null;
        List<WORKER> _listWorker = null;
        public DialogWorker(STATION sta)
        {
            if(sta==null)
            {
                MessageBox.Show("传入参数为空");
                this.Close();
            }
            _sta = sta;
            InitializeComponent();
        }
        /// <summary>
        /// 查询员工信息的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            this.listView.Items.Clear();
            if(this._listWorker!=null)
                this._listWorker.Clear();

            long workerId = 0;
            WORKER workerWhere = new WORKER();
            workerWhere.SetWhere();
            if (long.TryParse(this.textBoxWorkId.Text,out workerId))//使用员工号查询员工数据信息
            {
                workerWhere.ID = workerId;
                workerWhere.FLAG = true;//在职
                var workerRes = MethodWcf.SelectWorkerClient(workerWhere);
                if (workerRes.result) _listWorker = workerRes.value;
            }

            string workerName = this.textBoxWorkerName.Text;
            if ((_listWorker == null||_listWorker.Count==0)&&!string.IsNullOrEmpty(workerName))//使用员工姓名查询数据信息
            {
                workerWhere.Clear();
                workerWhere.FLAG = true;//在职
                workerWhere.NAME = workerName;

                var workerRes = MethodWcf.SelectWorkerClient(workerWhere);
                if (workerRes.result) _listWorker = workerRes.value;
            }

            //给listView赋值
            if(_listWorker!=null && _listWorker.Count >= 1)
            {
                for (int i = 0; i < _listWorker.Count; i++)
                {
                    WORKER worker = _listWorker[i];

                    ListViewItem item = new ListViewItem();
                    item.Text = worker.ID.ToString();
                    item.ImageIndex = 1;
                    item.SubItems.Add(worker.NAME);
                    item.SubItems.Add(worker.BIRTHDAY.ToString());

                    if (!string.IsNullOrEmpty(worker.CARDID)&&worker.CARDID.Length>=18)//员工身份证不为空
                        item.SubItems.Add(worker.CARDID.Remove(0, 12));
                    else
                        item.SubItems.Add("身份证未填写");

                    item.SubItems.Add(worker.CONTEXT);
                    item.SubItems.Add(worker.FLAG.ToString());
                    item.SubItems.Add(worker.ENTER_DATE.Value.ToShortDateString());
                    this.listView.Items.Add(item);
                }
            }
        }

        private void listView_MouseClick(object sender, MouseEventArgs e)
        {
            if (this._listWorker.Count == 1)
            {
                _sta.WORKER_ID = _listWorker[0].ID;
                _sta.WORKER_NAME = _listWorker[0].NAME;
                this.Close();
            }

            var selected = this.listView.SelectedItems;
            if (this._listWorker.Count >= 1 && selected.Count == 1)//ITEM的数量大于1
            {
                //查到相应的Line信息
                long workerId = long.Parse(selected[0].Text);
                var worker = (from l in _listWorker
                            where l.ID == workerId
                            select l).FirstOrDefault();

                _sta.WORKER_ID = worker.ID;
                _sta.WORKER_NAME = worker.NAME;

                this.Close();
            }
        }

    }
}
